package com.acme.bankapp.service.bank;

import java.util.ArrayList;

import com.acme.bankapp.domain.bank.*;

import com.acme.bankapp.exceptions.bank.*;

public class BankService {
	
	public static void addClient (Bank bank, Client client) throws ClientExistsException{
		ArrayList<Client> al = bank.getClients();
		for(Client cl : al){
			if(cl.getName().equals(client.getName())){
				throw new ClientExistsException("Client with such name already exists");
			}
		}
		bank.setClients(client);
		for(ClientRegisrationListener listener : bank.getListeners()){
			listener.onClientAdded(client);
		}
	}
	
	public static void addAccount(Bank bank, int idClient, int ... initialValueAndOverDraft){
		
		if(initialValueAndOverDraft.length == 1){
			Account savAcc = new SavingsAccount(initialValueAndOverDraft[0]);
			ArrayList<Client> cl = bank.getClients();
			if(cl.size() < idClient){
				System.out.println("Wrong format input data");
			}
			else{
				cl.get(idClient).getAccounts().add(savAcc);
			}
		}
		else if(initialValueAndOverDraft.length == 2){
			Account checkAcc = new CheckingAccount(initialValueAndOverDraft[0],
					initialValueAndOverDraft[1]);
			ArrayList<Client> cl = bank.getClients();
			if(cl.size() < idClient){
				System.out.println("Wrong format input data");
			}
			else{
				cl.get(idClient).getAccounts().add(checkAcc);
			}
		}
		else{
			System.out.println("Wrong format input data");
		}
	}
}
